package com.springboot.controller;

import com.springboot.model.Goods;
import com.springboot.model.User;
import com.springboot.service.GoodsService;
import org.apache.shardingsphere.api.hint.HintManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Author: TongRui乀
 * @Date: 2019/10/16 13:55
 * @description：
 */
@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;


    @GetMapping("getGoodsById")
    public Goods getGoodsById(Integer id, Integer tenantId){

        HintManager hintManager = HintManager.getInstance();

        hintManager.addDatabaseShardingValue("goods", tenantId);

        return goodsService.getGoodsById(id);
    }


    @GetMapping("getUserById")
    public User add(Integer id, String tenantId){

        HintManager instance = HintManager.getInstance();

        instance.setDatabaseShardingValue(tenantId);

        return this.goodsService.getUserById(id);
    }

    @PostMapping("insertUser")
    public String insertUser(User user){

        this.goodsService.insertUser(user);

        return "SUCCESS";
    }

    @PostMapping("insertGoods")
    public String insertGoods(Goods goods){

        this.goodsService.insertGoods(goods);

        return "SUCCESS";
    }

    @PutMapping("updateUser")
    public String updateUser(User user){

        this.goodsService.updateUser(user);

        return "SUCCESS";
    }

    @PutMapping("updateGoods")
    public String updateGoods(Goods goods){

        this.goodsService.updateGoods(goods);

        return "SUCCESS";
    }

}
